package cn.com.huak.www.sell.controller;

import cn.com.huak.www.sell.converter.OrderForm2OrderDTOConverter;
import cn.com.huak.www.sell.dataObject.OrderMaster;
import cn.com.huak.www.sell.dto.OrderDTO;
import cn.com.huak.www.sell.enums.ResultEnum;
import cn.com.huak.www.sell.exception.OrderException;
import cn.com.huak.www.sell.form.OrderForm;
import cn.com.huak.www.sell.service.BuyerService;
import cn.com.huak.www.sell.service.OrderMasterService;
import cn.com.huak.www.sell.utils.ResultVoUtil;
import cn.com.huak.www.sell.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;

/**
 * @author huak@wanggang
 * @date 2019/1/1 23:00
 */
@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyerOrderController {

    @Autowired
    private OrderMasterService orderMasterService;

    @Autowired
    private BuyerService buyerService;

    /* 创建订单 */
    @PostMapping("/create")
    public ResultVo create(@Valid OrderForm orderForm,
                           BindingResult bindingResult){
        //1. 参数验证
        if(bindingResult.hasErrors()){
            log.error("【创建订单】参数不正确;form={}", orderForm);
            throw new OrderException(ResultEnum.ORDER_FORM_ERROR.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
        }
        //2. 调用业务层
        OrderDTO convert = OrderForm2OrderDTOConverter.convert(orderForm);
        if(null == convert.getOrderDetailList() || convert.getOrderDetailList().size() <= 0){
            log.error("【创建订单】购物车为空....");
            throw new OrderException(ResultEnum.ORDER_CART_EMPTY);
        }
        convert = orderMasterService.create(convert);

        //3. 返回结果信息
        HashMap<String, String> map = new HashMap<>();
        map.put("orderId", convert.getId());
        return ResultVoUtil.success(map);
    }

    /* 查询单个订单 */
    @GetMapping("/detail")
    public ResultVo detail(@RequestParam("openid") String openid,
                           @RequestParam("orderId") String orderId){
        //  安全性的问题
        //1. 调用业务层
        OrderDTO one = buyerService.findOrderOne(openid, orderId);
        //OrderDTO one = orderMasterService.findOne(orderId);

        //2. 返回结果
        return ResultVoUtil.success(one);
    }

    /* 订单列表 */
    @GetMapping("/list")
    public ResultVo findList(@RequestParam("openid") String openid,
                             @RequestParam(value="page", defaultValue = "0") Integer page,
                             @RequestParam(value="page", defaultValue = "10") Integer size){
        if(StringUtils.isEmpty(openid)){
            log.error("【查询订单列表】openid为空....");
            throw new OrderException(ResultEnum.OPENID_ERROR);
        }

        //1. 调用业务层
        Page<OrderDTO> list = orderMasterService.findList(openid, new PageRequest(page, size));

        //2. 返回结果
        return ResultVoUtil.success(list.getContent());
    }

    /* 取消订单 */
    @PostMapping("/cancel")
    public ResultVo cancel(@RequestParam("openid") String openid,
                           @RequestParam("orderId") String orderId){
        //1.   安全性问题
        //2. 调用业务层
        buyerService.cancel(openid, orderId);
        //3. 返回结果
        return ResultVoUtil.success();
    }
}
